﻿using HslCommunication;
using HslCommunication.Core;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UYD_WCS_AS_AGV_Controller.AGV.Fork
{
    /// <summary>
    /// 使用 西门子1200 PLC ，通过网口进行数据传输,使用S7协议；堆垛机使用
    /// </summary>
    public partial class PLCOperate_AgvFork
    {
        private SiemensS7Net siemensTcpNet = null;
        private SiemensPLCS siemensPLCSelected = SiemensPLCS.S1200;
        private IReadWriteNet readWriteNet;
        private MethodInfo readByteMethod = null;
        private MethodInfo writeByteMethod = null;

        //public PLCMsgModel plcMsg = new PLCMsgModel();

        public PLCOperate_AgvFork()
        {
            SiemensPLCS siemensPLCS = SiemensPLCS.S1200;
            siemensPLCSelected = siemensPLCS;
            siemensTcpNet = new SiemensS7Net(siemensPLCS);
        }
        public PLCMsgModel PLCInit(string ip,string port,string rack,string slot)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            // 连接
            siemensTcpNet.IpAddress = ip;
            siemensTcpNet.Port = int.Parse(port);
            siemensTcpNet.Rack = byte.Parse(rack);
            siemensTcpNet.Slot = byte.Parse(slot);
            try
            {
                OperateResult connect = siemensTcpNet.ConnectServer();
                if (connect.IsSuccess)
                {
                    readWriteNet = siemensTcpNet;
                    Type type = siemensTcpNet.GetType();
                    readByteMethod = type.GetMethod("ReadByte", new Type[] { typeof(string) });

                    try
                    {
                        writeByteMethod = type.GetMethod("Write", new Type[] { typeof(string), typeof(byte) });
                    }
                    catch
                    {
                    }
                    plcMsg.Code = 1;
                    plcMsg.Msg = "AGV上货叉PLC连接成功!";
                }
                else
                {
                    plcMsg.Code = 0;
                    plcMsg.Msg = "AGV上货叉PLC连接失败!";
                }
            }
            catch (Exception ex)
            {
                plcMsg.Code = 0;
                plcMsg.Msg = "AGV上货叉PLC连接异常!" + ex.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadBool(string addr)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<bool> operRe = readWriteNet.ReadBool(addr);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadBoolArray(string addr,ushort len)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<bool[]> operRe = readWriteNet.ReadBool(addr,len);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadByte(string addr)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<short> operRe = readWriteNet.ReadInt16(addr);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadByte(string addr ,ushort len)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<short[]> operRe = readWriteNet.ReadInt16(addr,len);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadInt(string addr)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<int> operRe = readWriteNet.ReadInt32(addr);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCReadInt(string addr, ushort len)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult<int[]> operRe = readWriteNet.ReadInt32(addr, len);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = operRe.Content;
                plcMsg.Msg = "PLC读取成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC读取失败！" + operRe.Message;
            }
            return plcMsg;
        }

        public PLCMsgModel PLCWrite(string addr, bool val)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult operRe = readWriteNet.Write(addr, val);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC写入成功！";
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC写入失败！" + operRe.Message;
            }
            return plcMsg;
        }


        public PLCMsgModel PLCWrite(string addr, short val)
        {
            PLCMsgModel plcMsg = new PLCMsgModel();
            OperateResult operRe = readWriteNet.Write(addr, val);
            if (operRe.IsSuccess)
            {
                plcMsg.Code = 1;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC写入成功！";
                Thread.Sleep(200);
            }
            else
            {
                plcMsg.Code = 0;
                plcMsg.Val = null;
                plcMsg.Msg = "PLC写入失败！"+operRe.Message;
                Thread.Sleep(200);
            }
            return plcMsg;
        }

    }

}
